use common::error::MyError;
use entity::role;
use model::role::{CreateRole, RolePage, SetRoleResource};

use crate::base::BaseService;
use crate::manager::role::RoleManager;

pub struct RoleService {}

impl BaseService<i64, role::Model, RolePage, CreateRole, role::Model, RoleManager> for RoleService {}

impl RoleService {
    pub async fn get_resource(role_id: i64) -> Result<Vec<i64>, MyError> {
        Ok(RoleManager::get_resource_ids_by_role_ids(vec![role_id]).await?)
    }

    pub async fn set_resource(set_role_resource: SetRoleResource) -> Result<bool, MyError> {
        let role_id = set_role_resource
            .role_id
            .ok_or(MyError::ServerError("角色ID不能为空".to_string()))?;
        let resource_ids = set_role_resource
            .resource
            .ok_or(MyError::ServerError("资源ID不能为空".to_string()))?;
        Ok(RoleManager::set_role_resource(resource_ids, role_id).await?)
    }
}
